import { handleActions } from 'redux-actions'
import { GETLIST, CHANGELISTSTATUS, REMOVE, DOWNNEWSTATUS } from '../types/indexList'
import { randomNum } from '../../util/index'
const defaultState = {
	list: [],
	downNew: false
}
export default handleActions({
	[GETLIST](state, action) {
		action.payload[0].studyPackageList.forEach(element => {
			element.stydyNumber = randomNum(300, 100)
		})
		state.list.push.apply(state.list, action.payload[0].studyPackageList)
		return {
			...state
		}
	},
	[CHANGELISTSTATUS](state, action) {
		let i = action.payload
		i && state.list.forEach((item, index) => {
			if (item.stuPackageId !== i.id && item.studyType === 1 && i.studyType === 1) {
				state.list[index].studyType = 0
			}
			if (item.stuPackageId === i.id) {
				state.list[index].studyType = i.studyType
			}
		})
		return {
			...state
		}
	},
	[REMOVE](state, action) {
		return {
			...state,
			list: action.payload
		}
	},
	[DOWNNEWSTATUS](state, action) {
		return {
			...state,
			downNew: action.payload
		}
	}
}, defaultState)
